package com.neusoft.cmsprojects.action;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.neusoft.cmsprojects.model.Page;
import com.neusoft.cmsprojects.util.PageUtil;
import com.jspsmart.upload.SmartUploadException;
import com.neusoft.cmsprojects.util.FileUtil;
import com.neusoft.cmsprojects.util.PictuerUploadDeleteUtil;
import com.neusoft.cmsprojects.model.CProductsorta;
import com.neusoft.cmsprojects.model.CProductsortb;
import com.neusoft.cmsprojects.model.ProductList;
import com.neusoft.cmsprojects.model.ProductTypeList;
import com.neusoft.cmsprojects.model.TbProduct;
import com.neusoft.cmsprojects.model.TbUser;
import com.neusoft.cmsprojects.service.ProductTypeService;
import com.opensymphony.xwork2.ActionSupport;



/**index 产品类
 * @author liuyanhui
 *
 */
public class ProductAction extends ActionSupport{
	
     /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private CProductsorta productsorta;
	private File file;
	private String fileFileName;
	private String fileContentType;
	private String oldPhoto;
	private String CName;
	private String fileName;
	private String CNameA;
	private String CNameB;
	private String CNameT;
	private String description;
	private int productid;
    private CProductsortb productsortb;
    private TbProduct product;
    private String opt;
    private int currentPage;
    @Resource(name="ProductTypeService")
    private ProductTypeService producttypeservice;
	
    //保存产品
    public void saveproduct() throws IOException{
 		 if(product.getCId()!=null){//传过来的ID为空不为NULL就是修改产品
 			TbProduct oldProduct=this.producttypeservice.queryProductById(TbProduct.class,product.getCId());
 			System.out.println("旧产品"+oldProduct);
 		 if(oldProduct==null){//没有该产品既是添加产品
    	     this.addProduct();
    	 }else{
    	     this.modifyProduct(oldProduct);
    	}
 		 }else{//ID为NULL是增加产品
 			 this.addProduct(); 
 		 }
 		 
 		 
    	
    }
    //修改产品
    public void modifyProduct(TbProduct oldProduct){
    	 TbUser tbUser = (TbUser) ServletActionContext.getRequest().getSession().getAttribute("user");
    	 Date d = new Date();
    	 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
 		 String strdate = sdf.format(new Date());
    	description = description.replaceAll("src=\"tempfiles/" + strdate
				+ "/", "src=\"/CMSProject/Resources/image/"); // 改变<img/>标签的图片路径
		
		oldProduct.setCDescription(description.getBytes());
		oldProduct.setCModifier(tbUser.getCid());
		oldProduct.setCModifytime(d);
		oldProduct.setCSortbid(this.producttypeservice.queryPbByCname(CNameT).get(0).getId());
	    this.producttypeservice.updateproduct(oldProduct);
    }
    
    //添加产品
    public void addProduct() throws IOException{
    	 TbUser tbUser = (TbUser) ServletActionContext.getRequest().getSession().getAttribute("user");
    	 Date d = new Date();
    	 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
 		 String strdate = sdf.format(new Date());
		description = description.replaceAll("src=\"tempfiles/" + strdate
				+ "/", "src=\"/CMSProject/Resources/image/"); // 改变<img/>标签的图片路径
		FileUtil.saveImageFromText("tempfiles\\"+strdate+"/", "\\Resources\\image\\", description, "src=\"/CMSProject/Resources/image/(.*?)\"");
		product.setCDescription(description.getBytes());
		product.setCCreator(tbUser.getCid());
	    product.setCCreatetime(d);
	    product.setCSortbid(this.producttypeservice.queryPbByCname(CNameT).get(0).getId());
    	this.producttypeservice.saveproduct(product);
    }
    //删除产品
    public void  deleteProduct(){
    	TbProduct delProduct=this.producttypeservice.queryProductById(TbProduct.class,productid);
    	if(delProduct!=null){
    		FileUtil.deleteImageFromText("Resources\\image\\", new String(
    				delProduct.getCDescription()),	
    				"src=\"/CMSProject/Resources/image/(.*?)\"");
    	}
    	this.producttypeservice.deleteProduct(delProduct);
    }
    
     //条件查询
    public String queryByCond() throws UnsupportedEncodingException{
    	
    	if(CName!=null){CName = new String(CName.getBytes("ISO8859_1"), "UTF8");}
    	if(CNameB!=null){CNameB = new String(CNameB.getBytes("ISO8859_1"), "UTF8");}
    /*	System.out.println(productid);
    	System.out.println(CName);
    	System.out.println(CNameB);
    	System.out.println(opt);*/
    	if(opt.trim().equals("id")){
    		TbProduct product=this.producttypeservice.queryProductById(TbProduct.class, productid);
    	    List<TbProduct> productlist=new ArrayList<TbProduct>();
    	    productlist.add(product);
    	    this.ChangeModel(productlist);
    	}else if(opt.trim().equals("name")){
    		
    		List<TbProduct> productlist=this.producttypeservice.queryProductByCName(CName);
    		this.ChangeModel(productlist);
    	}else if(opt.trim().equals("nameB")){
    		List<CProductsortb> productsortblist=this.producttypeservice.queryPbByCname(CNameB);
    		if(productsortblist.size()!=0){
    			CProductsortb productsortb=this.producttypeservice.queryPbByCname(CNameB).get(0);
    			List<TbProduct> productlist=this.producttypeservice.queryProductBySortbId(productsortb.getId());
    			this.ChangeModel(productlist);
    		}
    		
    		
    		
    	}
    	return "AllProduct";
    }
    
    // 添加一级种类
   public void AddType1(){
	   if(productsorta!=null){
	   this.producttypeservice.addtype(productsorta);}
	  
   }
   //添加二级种类
   public void AddType2() throws UnsupportedEncodingException{
	   CName = new String(CName.getBytes("ISO8859_1"), "UTF8");
	   CProductsorta CPA=this.producttypeservice.queryPaByCname(CName).get(0);
	   TbUser tbUser = (TbUser) ServletActionContext.getRequest().getSession().getAttribute("user");
  	   Date d = new Date();
  	   System.out.println(this.productsortb.getId());
  	   CProductsortb CPB=this.producttypeservice.querySortBById(CProductsortb.class,this.productsortb.getId());
	  /* List<CProductsortb> CPBList=this.producttypeservice.queryPbByCname(this.productsortb.getCName());*/
	   System.out.println(CPB);
	   if(CPB==null){//判断是否存在该二级种类,不存在就新增
		   System.out.println("新增");
	   productsortb.setCSortaid(CPA.getId());
	   productsortb.setCCreator(tbUser.getCid());
	   productsortb.setCCreatetime(d);
	   this.producttypeservice.addtype(productsortb);}
	   else {
		   System.out.println("修改");
		   CPB.setCModifier(tbUser.getCid());
		   CPB.setCModifytime(d);
		   CPB.setCName(this.productsortb.getCName());
		   CPB.setCNamee(this.productsortb.getCNamee());
		   CPB.setCDescription(this.productsortb.getCDescription());
		   this.producttypeservice.updatetypeb(CPB);
	   }
	 
	   
   }
   
   
   //图片加载
   public void ProductPicture(){
	   System.out.println(file);
   }
   //把三个表组合成一个表
   public void ChangeModel(List<TbProduct> product){
	   HttpServletRequest request=ServletActionContext.getRequest();
	   List<ProductList> PL=new ArrayList<ProductList>();
	   for(int i=0;i<product.size();i++){
		   System.out.println(i);
		   ProductList pl=new ProductList();
		   pl.setCId(product.get(i).getCId());
		   pl.setCCode(product.get(i).getCCode());
		   pl.setCCreatetime(product.get(i).getCCreatetime());
		   pl.setCCreator(product.get(i).getCCreator());
		   pl.setCDescription(product.get(i).getCDescription());
		   pl.setCDescriptione(product.get(i).getCDescriptione());
		   pl.setCIntro(product.get(i).getCIntro());
		   pl.setCIntroe(product.get(i).getCIntroe());
		   pl.setCModifier(product.get(i).getCModifier());
		   pl.setCModifytime(product.get(i).getCModifytime());
		   pl.setCName(product.get(i).getCName());
		   pl.setCNamee(product.get(i).getCNamee());
		   System.out.println(product.get(i).getCSortbid());
		   CProductsortb cproductsortb=this.producttypeservice.getoneCPB(CProductsortb.class, product.get(i).getCSortbid());
		   System.out.println(cproductsortb);
		   pl.setCNameA(this.producttypeservice.getoneCPA(CProductsorta.class, cproductsortb.getCSortaid()).getCName());
		   pl.setCNameeA(this.producttypeservice.getoneCPA(CProductsorta.class, cproductsortb.getCSortaid()).getCNamee());
		   pl.setCNameB(this.producttypeservice.getoneCPB(CProductsortb.class, product.get(i).getCSortbid()).getCName());
		   pl.setCNameeB(this.producttypeservice.getoneCPB(CProductsortb.class, product.get(i).getCSortbid()).getCNamee());
		   if(product.get(i).getCDescription()!=null){
			   request.setAttribute("CDescription",new String (product.get(i).getCDescription()));
		   }
		   PL.add(pl);
	   }
	   Page page = PageUtil.createPage(10, PL.size(), currentPage);
	    int endPage = page.getBeginIndex() + page.getEveryPage();
	    if(page.getBeginIndex() + page.getEveryPage() > page.getTotalCount()) {
	    	endPage = page.getTotalCount();
	    }
	    PL = PL.subList(page.getBeginIndex(), endPage);
	    request.setAttribute("page",page);
	   request.setAttribute("product",PL);
   }
   
   
   //查找所有产品返回AllProduct.jsp
   public String AllProduct(){
	   List<TbProduct> product=this.producttypeservice.queryAllProduct();//产品原表
	   System.out.println("产品数量----"+product.size());
	   this.ChangeModel(product);
	   return "AllProduct";
   }
 //图片预览
 		public String uploadPicture() throws IOException{
 			HttpServletRequest request = ServletActionContext.getRequest();
 		
 			if(!oldPhoto.equals("")){//删除旧照片
 				this.deletePhoto();
 			}
 			
 			String fileName = PictuerUploadDeleteUtil.uploadPicture(this.file);	//上传图片
 			request.setAttribute("fileName",fileName);
 			return "userdata";
 		}
 		
 		//删除照片
 				public void deletePhoto(){
 					//System.out.println(oldPhoto+"-----------0122");
 					PictuerUploadDeleteUtil.deletePicture(oldPhoto);
 				}
   //查找一级产品类型
   public String QueryType1 (){
	   HttpServletRequest request=ServletActionContext.getRequest();
	   List<CProductsorta> CPA=this.producttypeservice.queryPA();
	   request.setAttribute("CPA",CPA);
	   return "QueryType1";
   }
   //删除产品类型
   public void deletedata() throws UnsupportedEncodingException{
	   System.out.println(CNameB);
	   CNameB = new String(CNameB.getBytes("ISO8859_1"), "UTF8");
	   List<CProductsortb> CPB=this.producttypeservice.queryPbByCname(CNameB);
	   this.producttypeservice.delete(CPB.get(0));
	   
   }
   public String ModifyProduct(){
	   HttpServletRequest request=ServletActionContext.getRequest();
	   TbProduct tbproduct=this.producttypeservice.getoneProduct(TbProduct.class,productid);
	   System.out.println(tbproduct);
	   if(tbproduct!=null){
		   ProductList pl=new ProductList();
		   pl.setCId(tbproduct.getCId());
		   pl.setCCode(tbproduct.getCCode());
		   pl.setCCreatetime(tbproduct.getCCreatetime());
		   pl.setCCreator(tbproduct.getCCreator());
		   pl.setCDescription(tbproduct.getCDescription());
		   pl.setCDescriptione(tbproduct.getCDescriptione());
		   pl.setCIntro(tbproduct.getCIntro());
		   pl.setCIntroe(tbproduct.getCIntroe());
		   pl.setCModifier(tbproduct.getCModifier());
		   pl.setCModifytime(tbproduct.getCModifytime());
		   pl.setCName(tbproduct.getCName());
		   pl.setCNamee(tbproduct.getCNamee());
		   System.out.println(this.producttypeservice.getoneCPB(CProductsortb.class, tbproduct.getCSortbid()));
		   CProductsortb cproductsortb=this.producttypeservice.getoneCPB(CProductsortb.class, tbproduct.getCSortbid());
		   pl.setCNameA(this.producttypeservice.getoneCPA(CProductsorta.class, cproductsortb.getCSortaid()).getCName());
		   pl.setCNameeA(this.producttypeservice.getoneCPA(CProductsorta.class, cproductsortb.getCSortaid()).getCNamee());
		   pl.setCNameB(this.producttypeservice.getoneCPB(CProductsortb.class, tbproduct.getCSortbid()).getCName());
		   pl.setCNameeB(this.producttypeservice.getoneCPB(CProductsortb.class, tbproduct.getCSortbid()).getCNamee());
		   if(tbproduct.getCDescription()!=null){
			   request.setAttribute("CDescription",new String (tbproduct.getCDescription()));
		   }
		   System.out.println(pl.getCName());
		   request.setAttribute("product",pl);
	   }
	   return "AddProduct";
   }
   
   
   //读取产品类别并返回Product.jsp
   public String AddProduct(){
	   HttpServletRequest request=ServletActionContext.getRequest();
	   List<CProductsortb> cpblist= this.producttypeservice.queryPB();
	   request.setAttribute("protype",cpblist);
	   return "AddProduct";
   }
   
   //添加产品二级种类
   public String AddProductType() throws UnsupportedEncodingException{
	   HttpServletRequest request=ServletActionContext.getRequest();
	   if(CNameB!=null){CNameB = new String(CNameB.getBytes("ISO8859_1"), "UTF8");}
	   List<CProductsortb> CPB=this.producttypeservice.queryPbByCname(CNameB);
	   ProductTypeList producttype=new ProductTypeList();
	   producttype.setCId(CPB.get(0).getId());
	   producttype.setCNameB(CPB.get(0).getCName());
	   producttype.setCNameeB(CPB.get(0).getCNamee());
	   producttype.setCCreator(CPB.get(0).getCCreator());
	   producttype.setCCreatetime(CPB.get(0).getCCreatetime());
	   producttype.setCModifier(CPB.get(0).getCModifier());
	   producttype.setCModifytime(CPB.get(0).getCModifytime());
	   producttype.setCDescription(CPB.get(0).getCDescription());
	   producttype.setCNameA(this.producttypeservice.getoneCPA(CProductsorta.class, CPB.get(0).getCSortaid()).getCName());
	   producttype.setCNameeA(this.producttypeservice.getoneCPA(CProductsorta.class, CPB.get(0).getCSortaid()).getCNamee());
	   producttype.setCDescription(CPB.get(0).getCDescription());
	   request.setAttribute("ptl",producttype);
	   return "QueryType1";
   }
   //把一级和二级种类合成两个表
   public void ChangeModel2(List<CProductsortb> CPB){
	   HttpServletRequest request=ServletActionContext.getRequest();
	   List<ProductTypeList> PTL=new ArrayList<ProductTypeList>();
	   for(int i=0;i<CPB.size();i++){
		   ProductTypeList producttype=new ProductTypeList();
		   producttype.setCNameB(CPB.get(i).getCName());
		   producttype.setCNameeB(CPB.get(i).getCNamee());
		   producttype.setCCreator(CPB.get(i).getCCreator());
		   producttype.setCCreatetime(CPB.get(i).getCCreatetime());
		   producttype.setCModifier(CPB.get(i).getCModifier());
		   producttype.setCModifytime(CPB.get(i).getCModifytime());
		   producttype.setCDescription(CPB.get(i).getCDescription());
		   producttype.setCNameA(this.producttypeservice.getoneCPA(CProductsorta.class, CPB.get(i).getCSortaid()).getCName());
		   producttype.setCNameeA(this.producttypeservice.getoneCPA(CProductsorta.class, CPB.get(i).getCSortaid()).getCNamee());
		   producttype.setCDescription(CPB.get(i).getCDescription());
		   PTL.add(producttype);
		   
	   }
	   Page page = PageUtil.createPage(10, PTL.size(), currentPage);
	    int endPage = page.getBeginIndex() + page.getEveryPage();
	    if(page.getBeginIndex() + page.getEveryPage() > page.getTotalCount()) {
	    	endPage = page.getTotalCount();
	    }
	    PTL = PTL.subList(page.getBeginIndex(), endPage);
	    request.setAttribute("page",page);
	   request.setAttribute("PTL",PTL);
	   
   }
   //条件查找产品类型
   public String QueryTypeByCond() throws UnsupportedEncodingException{
	   if(CNameA!=null){CNameA = new String(CNameA.getBytes("ISO8859_1"), "UTF8");}
	   if(CNameB!=null){CNameB = new String(CNameB.getBytes("ISO8859_1"), "UTF8");}
	   List<CProductsorta> cproductsorta=this.producttypeservice.queryPaByCname(CNameA);
	   if(opt.trim().equals("sortA")){}
	   if(cproductsorta.size()!=0){
		   List<CProductsortb> cproductsortb=this.producttypeservice.querySortBBySortAId(cproductsorta.get(0).getId());
		   this.ChangeModel2(cproductsortb);
	   }else if(opt.trim().equals("sortB")){
		   List<CProductsortb> cproductsortb=this.producttypeservice.queryPbByCname(CNameB);
		   this.ChangeModel2(cproductsortb);
	   }
	   
	   return "QueryType2";
   }
   
   //查询二级种类
   public String  QueryType2 (){
	   List<CProductsortb> CPB=this.producttypeservice.queryPB();
	   this.ChangeModel2(CPB);
	   return "QueryType2";
   }
   
   public void delType(){
	   this.producttypeservice.delete(productsortb);
	   
   }
public CProductsorta getProductsorta() {
	return productsorta;
}


public void setProductsorta(CProductsorta productsorta) {
	this.productsorta = productsorta;
}


public CProductsortb getProductsortb() {
	return productsortb;
}


public void setProductsortb(CProductsortb productsortb) {
	this.productsortb = productsortb;
}


/**
 * @return the producttypeservice
 */
public ProductTypeService getProducttypeservice() {
	return producttypeservice;
}


/**
 * @param producttypeservice the producttypeservice to set
 */
public void setProducttypeservice(ProductTypeService producttypeservice) {
	this.producttypeservice = producttypeservice;
}


/**
 * @return the serialversionuid
 */
public static long getSerialversionuid() {
	return serialVersionUID;
}
/**
 * @return the cName
 */
public String getCName() {
	return CName;
}
/**
 * @param cName the cName to set
 */
public void setCName(String cName) {
	CName = cName;
}
/**
 * @return the cNameB
 */
public String getCNameB() {
	return CNameB;
}
/**
 * @param cNameB the cNameB to set
 */
public void setCNameB(String cNameB) {
	CNameB = cNameB;
}



/**
 * @return the product
 */
public TbProduct getProduct() {
	return product;
}



/**
 * @param product the product to set
 */
public void setProduct(TbProduct product) {
	this.product = product;
}






/**
 * @return the file
 */
public File getFile() {
	return file;
}



/**
 * @param file the file to set
 */
public void setFile(File file) {
	this.file = file;
}



/**
 * @return the oldPhoto
 */
public String getOldPhoto() {
	return oldPhoto;
}



/**
 * @param oldPhoto the oldPhoto to set
 */
public void setOldPhoto(String oldPhoto) {
	this.oldPhoto = oldPhoto;
}



/**
 * @return the fileName
 */
public String getFileName() {
	return fileName;
}



/**
 * @param fileName the fileName to set
 */
public void setFileName(String fileName) {
	this.fileName = fileName;
}



/**
 * @return the fileFileName
 */
public String getFileFileName() {
	return fileFileName;
}



/**
 * @param fileFileName the fileFileName to set
 */
public void setFileFileName(String fileFileName) {
	this.fileFileName = fileFileName;
}



/**
 * @return the fileContentTpe
 */
public String getFileContentType() {
	return fileContentType;
}



/**
 * @param fileContentTpe the fileContentTpe to set
 */
public void setFileContentType(String fileContentType) {
	this.fileContentType = fileContentType;
}



/**
 * @return the description
 */
public String getDescription() {
	return description;
}



/**
 * @param description the description to set
 */
public void setDescription(String description) {
	this.description = description;
}


/**
 * @return the cNameT
 */
public String getCNameT() {
	return CNameT;
}


/**
 * @param cNameT the cNameT to set
 */
public void setCNameT(String cNameT) {
	CNameT = cNameT;
}


/**
 * @return the productid
 */
public int getProductid() {
	return productid;
}


/**
 * @param productid the productid to set
 */
public void setProductid(int productid) {
	this.productid = productid;
}

/**
 * @return the cNameA
 */
public String getCNameA() {
	return CNameA;
}

/**
 * @param cNameA the cNameA to set
 */
public void setCNameA(String cNameA) {
	CNameA = cNameA;
}
/**
 * @return the opt
 */
public String getOpt() {
	return opt;
}
/**
 * @param opt the opt to set
 */
public void setOpt(String opt) {
	this.opt = opt;
}
/**
 * @return the currentPage
 */
public int getCurrentPage() {
	return currentPage;
}
/**
 * @param currentPage the currentPage to set
 */
public void setCurrentPage(int currentPage) {
	this.currentPage = currentPage;
}





   
   
}
